<?php
/**
 * cms 信息控制器
 * @author maniac
 */
namespace app\manage\controller;

use app\common\model\Columns;
use app\common\model\MemberRoles;
use app\common\model\UserRoles;
use app\facade\Forms;
use app\facade\Contents AS ContentsFacade;

class Contents extends Base
{
    /**
     * 列表页补充
     * @param  array $data 列表页数据
     */
    public function indexAttach($data)
    {
        $param = $this->column_info();
        $this->attr();
        $list = ContentsFacade::listAttach($param['column_id'], $data);
        $this->assign($list);
    }
    
    /**
     * 添加/编辑补充
     * @param array $info 编辑的信息
     */
    public function addAttach($info)
    {

        $param  = $this->column_info();

        $roles  = MemberRoles::field('id,name')->order('id ASC')->select();
        $column = Columns::field('tbname,mod_id')->where('id', $param['column_id'])->find();

        $html   = Forms::forms($column['tbname'], $column['mod_id'], $info['id'], 'h');

        $this->assign('roles', $roles);
        $this->assign('html', $html);
        $this->attr();
    }
    
    /**
     * where 查询条件
     */
    public function where()
    {
        $column_id = input('get.column_id/d', 0);
        $ismember  = input('get.ismember/d', -1);
        $checked   = input('get.checked/d', -1);
        $isgood    = input('get.isgood/d', -1);
        $startTime = input('get.start_time/s', '');
        $endTime   = input('get.end_time/s', '');
        $keywords  = input('get.keywords/s', '');
        $province=input('province/d','-1');
        $city=input('city/d','-1');
        $district=input('district/d','-1');
        $where     = [];
        $roleId    = session('role_id', '', session_prefix());
        if ($roleId != 1) {
            $role = UserRoles::field('authorities')->where('id', $roleId)->find();
            if (in_array('/manage/contents/my', $role['authorities'])) {
                $where[] = [
                    ['userid', 'eq', session('userid', '', session_prefix())],
                ];
            }
        }
        if ($column_id) {
            $where[] = [
                ['column_id', 'eq', $column_id],
            ];
        }
        if ($ismember != '-1') {
            $where[] = [
                ['ismember', 'eq', $ismember],
            ];
        }
        if ($checked != '-1') {
            $where[] = [
                ['checked', 'eq', $checked],
            ];
        }
        if ($isgood != -1) {
            $where[] = [
                ['isgood', 'eq', $isgood],
            ];
        }
        if ($startTime) {
            $where[] = [
                ['post_time', 'egt', strtotime($startTime)],
            ];
        }
        if ($endTime) {
            $where[] = [
                ['post_time', 'lt', strtotime($endTime)],
            ];
        }
        if ($keywords) {
            $where[] = [
                ['title', 'like', '%'.$keywords.'%'],
            ];
        }

        if ($province != -1) {
            $where[] = [
                ['province', 'eq', $province],
            ];
        }

        if ($city != -1) {
            $where[] = [
                ['city', 'eq', $city],
            ];
        }

        if ($district != -1) {
            $where[] = [
                ['district', 'eq', $district],
            ];
        }

        $this->assign('ismember', $ismember);
        $this->assign('checked', $checked);
        $this->assign('isgood', $isgood);
        $this->assign('startTime', $startTime);
        $this->assign('endTime', $endTime);
        $this->assign('keywords', $keywords);
        return $where;
    }
    
    /**
     * 获取栏目信息
     */
    private function column_info()
    {
        $column_id   = input('column_id/d', 0);
        $column_name = Columns::where('id', $column_id)->value('name');
        $param       = [
            'column_id'   => $column_id,
            'column_name' => $column_name,
        ];
        $this->assign($param);
        return $param;
    }
    
    /**
     * 获取 cms 信息属性
     */
    private function attr()
    {
        $attr = config('attr.');
        $this->assign('attr', $attr);
    }
}
